$(function () {
    $("#jqGrid").jqGrid({
            url: baseURL + 'water/taccountmanage/statussuball',
            datatype: "json",
            colModel: [
                { label: '小区类型', name: 'proType', index: 'proType', width: 50 },
                { label: '区域', name: 'areaName', index: 'areaName', width: 80 },
                { label: '分所', name: 'unitName', index: 'unitName', width: 80 },
                { label: '小区', name: 'proName', index: 'proName', width: 80 },
                { label: '竣工日期', name: 'useDate', index: 'useDate', width: 80 },
                { label: '水表总数', name: 'scount', index: 'scount', width: 80 },
                { label: '质保内数', name: 'oknum', index: 'oknum', width: 80 },
                { label: '超期总数', name: 'over3', index: 'over3', width: 80 },
                { label: '超期三年', name: '', index: '', width: 80,
                    formatter: function(value, options, row){
                        return row.over3-row.over6;
                    }
                },
                { label: '超期六年', name: 'over6', index: 'over6', width: 80 }
            ],
		viewrecords: true,
        height: $(window).height()-130,
        rowNum: 3000,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        //multiselect: true,
        //pager: "#jqGridPager",
        jsonReader : {
            root: "page",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });

            //iframe自适应
            $(window).on('resize', function() {
                var $content = $('.content');
                $content.height($(this).height() - 154);
                $content.find('iframe').each(function() {
                    $(this).height($content.height());
                });
            }).resize();
        },
    });

    $(function () {
        $('#inputClear').click(function () {
            $('input')[0].value = "";
            vm.q.value="";
        })
    });
});


function zTreeOnDblClick(event, treeId, treeNode) {
    vm.q.dept = treeNode.deptId;
    vm.newpage = true;

    if(!treeNode.isParent) {
        var x = treeNode.deptId.split("_");
        if (x.length == 3){
            vm.q.dept = x[2];
            vm.reload();
        }
        // vm.reload();
    }

    //vm.reload();
    //alert(treeNode.deptId);
    //alert(treeNode ? treeNode.tId + ", " + treeNode.name : "isRoot");
};

var settingLeft = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    },
    callback: {
        onDblClick: zTreeOnDblClick
    }
};

var ztree;

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            key: '',
            value:null,
            dept:null,
            ckey: '',
        },
        newpage : false,
        searchKeyDictInfo: {},
		showList: true,
        showChild: false,
		title: null,
		vaccountmanage: {}
	},
	methods: {
        getDeptTreeLeft: function(){
            $.get(baseURL + "water/waterdept/queryCityAreaUnit", function(r){
                //console.log(r);
                ztree = $.fn.zTree.init($("#deptTreeLeft"), settingLeft, r);
                //开启模糊查询
                fuzzySearch('deptTreeLeft','#treeSearchKey',null,true); //初始化模糊搜索方法

                var node = ztree.getNodeByParam("deptId", vm.q.dept);
                if(node != null){
                    ztree.selectNode(node);
                    vm.q.dept = node.deptId;
                }
                //开启统计子节点数
                var temp=ztree.getNodes();
                getNum(ztree,temp);

                ztree.expandAll(true);
            })
        },
        outxls: function(){
            var total = $("#jqGrid").jqGrid('getGridParam', 'records') + "";
            var dept = vm.q.dept==null ? "" : vm.q.dept+  "";
            var url = baseURL
                + "water/vaccountmanage/export?params="
                + encodeURIComponent(JSON.stringify({'dept':dept,'limit':total,'page':"1"}));

            //console.log(decodeURIComponent(url));
            location.href = url;
        },
		reload: function (event) {
            //console.log(vm.q.dept);

			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
            if(vm.newpage){page = "1";}
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'dept':vm.q.dept},
                page:page
            }).trigger("reloadGrid");
        }
    },
    created: function(){
        this.getDeptTreeLeft();
    }
});